package alkaid.eis.schedule.core;

import java.util.concurrent.ScheduledFuture;

import alkaid.eis.schedule.domain.JobInfo.State;
import alkaid.eis.schedule.domain.JobLog.Level;
import lombok.Getter;

/**
 * ScheduledFuture的包装类
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@Getter
public class ScheduledFuctureHolder {
    private final ScheduledFuture<?> future;
    private final SchedulingTask task;

    public ScheduledFuctureHolder(SchedulingTask task, ScheduledFuture<?> future) {
        this.task = task;
        this.future = future;
    }

    /**
     * 取消定时任务
     */
    public void cancelTask() {
        if (future != null) {
            future.cancel(true);
        }

        task.jobInfoRepository.findById(task.jobInfoId)
                .ifPresent(jobInfo -> {
                    task.jobService.updateInfoState(jobInfo, State.STOPPED);
                    task.jobService.createLog(jobInfo, Level.STOPPED, task.formatMessage("任务关闭"));
                });
    }
}
